package search;

import java.util.Scanner;

            /**
             * 通过二分查找有序数组中的某个值
             * {1,2,3,4,5,6,7,8,9,10}
             */

public class BinarySearch {
    public static void main(String[] args) {
        int[] arr = {11,22,33,44,55,66};
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入要查找的值：");
        int num = scanner.nextInt();
        int res = Search(arr, num);
        System.out.println("查找的值在数组中的第"+(res+1)+"个位置");
    }

    public static int Search(int[] arr, int num) {
        int min = 0;
        int max = arr.length - 1;

        while (min <= max) {
            int mid = (min + max) / 2;
            if (arr[mid] < num) {
                min = mid + 1;
            } else if (arr[mid] > num) {
                max = mid - 1;
            } else {
                return mid;
            }
        }
        return -1;
    }
}
